//引入公共函数模块
let util = require('../../utils/util');

//获得请求域名
let requestDomain = util.config('requestDomain');

//存放当前页面实例
let that;

//页面Page函数
Page({

    //页面数据
    data: {
        registerCode: '',
        registerBtnDisabled: false,
        registered: false
    },

    //当页面加载完成时触发
    onLoad: function() {
        that = this;
        if(wx.getStorageSync('registered')) {
            that.setData({registered: wx.getStorageSync('registered')});
        }
    },

    //激活码输入函数
    registerCodeInput: function(e) {
        that.data.registerCode = e.detail.value;
    },
    
    registerAuth: function() {
        if(that.data.registerCode.length < 6) {
            wx.showModal({
                title: '系统提示',
                content: '请输入合法的激活码'
            });
            return;
        }
        that.setData({registerBtnDisabled: true});
        wx.showNavigationBarLoading();
        //先进行微信登录用于获取管理者openId
        wx.login({
          success: function(result){
            let loginCode = result.code;
            //发起激活权限请求
            wx.request({
                url: requestDomain + 'register/activation',
                data: {
                    loginCode: loginCode,
                    registerCode: that.data.registerCode
                },
                method: 'POST',
                success: function(result){
                    that.setData({registerBtnDisabled: false});
                    wx.hideNavigationBarLoading();
                    if(util.checkSuccess(result) && result.data && result.data.data) {
                        console.log(result.data.data);
                        let {storeId, storeName, serviceName, serviceAuth} = result.data.data;
                        util.globalData('newStoreId', storeId);
                        util.globalData('newStoreName', storeName);
                        let registered = {
                            storeId: storeId,
                            storeName: storeName
                        };
                        that.setData({registered: registered});
                        wx.setStorageSync('registered', registered);
                    }
                    else if(util.checkSuccess(result, ['-0149'])) {
                        wx.showModal({
                            title: '激活失败',
                            content: '此激活码无效，请检查'
                        });
                    }
                    else if(util.checkSuccess(result, ['-0158'])) {
                        wx.showModal({
                            title: '已激活',
                            content: '您的商户和您的账号已激活，无需重复激活'
                        });
                    }
                    else {
                        util.showError('激活权限失败', '系统在处理您的激活请求时出现问题' + result.data.errMsg, result.data.errCode);
                    }
                },
                fail: function(err) {
                    that.setData({registerBtnDisabled: false});              
                    wx.hideNavigationBarLoading();
                    console.log(err);
                    util.showError('请求失败', '发起激活权限请求失败');
                }
            });
          },
          fail: function(err) {
            console.log(err);
            that.setData({registerBtnDisabled: false});
            wx.hideNavigationBarLoading();
            util.showError('认证失败', '请求登录到微信服务器失败');
          }
        });
    },
    registerOther: function() {
        wx.showModal({
            title: '是否需要重新激活',
            content: '在重新激活之前请保证你已记住您的商户号，再激活新的权限，是否需要重新激活？',
            success: function(modal) {
                if(modal.confirm) {
                    that.setData({ registered: false });
                    wx.setStorageSync('registered', false);
                }
            }
        });
    }
});